<?php
/**
 * Created by linlingjie <1063944289@qq.com>.
 * Description:
 * Date: 2018/5/14
 * Time: 20:15
 */

namespace Admin\Controller;

use Think\Controller;

class BaseController extends Controller
{

    public function __construct()
    {
        //执行登录验证代码
        try {
            parent::__construct();
//      用于以后初始化执行方法
            if (!$teacher_id = session('adminId')) {
                $this->error('用户请重新登录', U('Login/login'), 3);
            } else {
                $teacher = M('teacher');
                $info = $teacher->find($teacher_id);
                if ($info) {
                    $this->assign('login_info', $info);
                } else {
                    $this->error('用户不存在，请重新登录', U('Login/login'), 3);
                }
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    protected $returnMessage = 'system error';
    /**
     * 返回代码
     * 0 成功
     * 1001 常规操作失败：新增、编辑、删除失败等
     * 1002 用户未登录
     * 1003 系统异常
     * 1004 数据为空：获取列表为空
     */
    protected $returnCode = '1003';
    /**
     * 返回数据列表
     */
    protected $returnInfo = [];
    protected $returnCount = 0;

    public function setReturnMessage($message)
    {
        $this->returnMessage = $message;
        return $this;
    }

    public function setReturnCode($code)
    {
        $this->returnCode = $code;
        return $this;
    }

    public function setReturnCount($count = 0)
    {
        $this->returnCount = $count;
        return $this;
    }

    public function setReturnInfo($data = [])
    {
        $this->returnInfo = $data;
        return $this;
    }

    /**
     * 成功返回操作，已默认code=1001
     * @param string $message 提示语
     * @param array $info 返回数据
     */
    public function successResponse($message = 'success', $info = [], $count = 0)
    {
        return $this->setReturnCode(0)->setReturnMessage($message)->setReturnInfo($info)->setReturnCount($count)->returnDo();
    }

    /**
     * 常规失败返回操作，已默认code=1002
     * @param string $message 提示语
     * @param array $info 返回数据
     */
    public function errorResponse($message = 'error', $info = [], $count = 0)
    {
        return $this->setReturnCode(1001)->setReturnMessage($message)->setReturnInfo($info)->setReturnCount($count)->returnDo();
    }

    /**
     * 查询到空数据返回
     * 默认code 1004
     * @param string $message
     * @return mixed
     */
    public function emptyResponse($message = 'no found')
    {
        return $this->setReturnCode(1004)->setReturnMessage($message)->setReturnInfo([])->setReturnCount()->returnDo();
    }

    /**
     * 通用返回数据
     * @param string $code 返回状态码，默认200
     * @param string $message 返回提示数据
     * @param array $info 返回数据
     */
    public function response($code = '1000', $message = 'success', $info = [], $count = 0)
    {
        return $this->setReturnCode($code)->setReturnMessage($message)->setReturnInfo($info)->setReturnCount($count)->returnDo();
    }

    /**
     * 返回接口数据
     */
    public function returnDo()
    {
        $return = [
            'code' => $this->returnCode,
            'msg' => $this->returnMessage,
            'count' => $this->returnCount,
            'data' => $this->returnInfo,
        ];
        //json，不转码
        echo json_encode($return, JSON_UNESCAPED_UNICODE);
    }

    /**
     * @param $teacher_id
     * @param $term_year
     * @param $workload_type
     */
    protected function dealWorkload($teacher_id, $term_year, $workload_type)
    {
        //workload_type: 1-coursetaks 2-designtask 3-projecttask
        if (!in_array($workload_type, [1, 2, 3])) {
            $this->error('工作量计算类型有误');
        }
        $Workload = M('workload');
        $where_teacher = [
            'teacher_id' => $teacher_id,
            'term' => $term_year,
            'is_del' => 0,
        ];
        $data = [];
        $all_fields = 'workload';
        $fields = '';
        $total_work = 0;
        switch ($workload_type) {
            case 1:
                $Coursetask = M('coursetask');
                $course_work = $Coursetask->where($where_teacher)->field('course_workload')->select();
                if (!empty($course_work)) {
                    foreach ($course_work as $value) {
                        $total_work += $value['course_workload'];
                    }
                }
                $data['course_workload'] = $total_work;
                $fields = 'course_workload';
                break;
            case 2:
                $Designtask = M('designtask');
                $design_work = $Designtask->where($where_teacher)->field('practice_workload')->select();
                if (!empty($design_work)) {
                    foreach ($design_work as $value) {
                        $total_work += $value['practice_workload'];
                    }
                }
                $data['practice_workload'] = $total_work;
                $fields = 'practice_workload';
                break;
            case 3:
                $Projecttask = M('projecttask');
                $project_work = $Projecttask->where($where_teacher)->count('id');
                if (!empty($project_work)) {
                    $total_work = $this->dealProjectload($project_work);
                }
                $data['project_workload'] = $total_work;
                $fields = 'project_workload';
                //更新毕设统计表
                $Projectworkload = M('projectworkload');
                $where_teacher_project = [
                    'teacher_id' => $teacher_id,
                    'term' => $term_year,
                ];
                $res = $Projectworkload->where($where_teacher_project)->field('id')->find();
                if (!empty($res)) {
                    $teacher_project_data = [
                        'student_number' => $project_work,
                        'project_workload' => $total_work,
                    ];
                    $resIII = $Projectworkload->where($res)->save($teacher_project_data);
                    if ($resIII === false) {
                        $this->error('毕设统计表更新失败');
                    }
                } else {
                    $Teacher = M('teacher');
                    $teacher_info = $Teacher->where('teacher_id = '. $teacher_id)->field('teacher_id, teacher_name')->find();
                    if (empty($teacher_info)) {
                        $this->error('teacher_id 对应教师不存在');
                    }
                    $teacher_project_data = [
                        'student_number' => $project_work,
                        'project_workload' => $total_work,
                        'teacher_id' => $teacher_id,
                        'teacher_name' => $teacher_info['teacher_name'],
                        'term' => $term_year,
                    ];
                    $Projectworkload->create();
                    $resIII = $Projectworkload->add($teacher_project_data);
                    if ($resIII === false) {
                        $this->error('毕设统计表新增失败');
                    }
                }
                break;
            default:
                $this->error('工作量计算类型有误');
                break;
        }
        unset($where_teacher['is_del']);

        $resI = $Workload->where($where_teacher)->field('id')->find();
        if ($resI) { //更新
            //计算总工作量
            if (empty($fields)) {
                $this->error('系统错误，统计表字段更新失败');
            }
            $all_fields = $all_fields . ',' . $fields;
            $all_workload = $Workload->where($resI)->field($all_fields)->find();
            $data['workload'] = $all_workload['workload'] - $all_workload[$fields] + $total_work;
            //更新表
            $resII = $Workload->where($resI)->save($data);
            if ($resII === false) {
                $this->error('工作量统计表更新失败');
            }
        } else { //新增
            if (empty($fields)) {
                $this->error('系统错误，统计表字段更新失败');
            }
            $Workload->create();
            $data['workload'] = $total_work;
            //查找老师信息以及学年信息
            $Teacher = M('teacher');
            $teacher_info = $Teacher->where('teacher_id = '. $teacher_id)->field('teacher_id, teacher_name')->find();
            if (empty($teacher_info)) {
                $this->error('teacher_id 对应教师不存在');
            }
            $data['teacher_id'] = $teacher_info['teacher_id'];
            $data['teacher_name'] = $teacher_info['teacher_name'];
            $data['term'] = $term_year;
            $resII = $Workload->add($data);
            if ($resII === false) {
                $this->error('工作量统计表新增失败');
            }
        }
    }

    /**
     * @param $count
     */
    protected function dealProjectload($count)
    {
        $Systerm = M('system');
        $resI = $Systerm->where('coe_id = 1')->field('coe_d')->find();
        if ($count >= 1 && $count <= $resI['coe_d']) {
            $A3 = $count;
        } elseif ($count > $resI['coe_d']) {
            $A3 = 8+($count-$resI['coe_d']) * 0.5;
        } else {
            $A3 = 0;
        }
        $workload = 15 * $A3;
        return $workload;
    }
}